﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpDX;
using SharpDX.Direct2D1;
using Write = SharpDX.DirectWrite;

namespace SFSpacefight
{
	public class Button : Control
	{
		public Button(RenderTarget target, String intext, int x, int y, int width, int height)
			: base(target)
		{
			text = intext;
			layoutrect = new DrawingRectangleF(x, y, width, height);
		}
		public override GameState Loop(int mousex, int mousey, bool clicked)
		{
			Boolean thisclicked = false;
			oldalpha = alpha;

			if (layoutrect.X <= mousex && layoutrect.X + layoutrect.Width >= mousex && layoutrect.Y <= mousey && layoutrect.Y + layoutrect.Height >= mousey)
			{
				alpha = 0.9F + alpha * 0.1F;
				if (clicked)
					thisclicked = true;
			}
			else
				alpha *= 0.4f;

			return thisclicked ? GameState.ButtonClicked : GameState.None;
		}
		public override void Draw(RenderTarget target, double percent)
		{
			Matrix3x2 backuptransform = target.Transform;
			float curalpha = Methods.linstep(oldalpha, alpha, percent);

			//persistent layer
			lightbrush.Opacity = mediumbrush.Opacity = darkbrush.Opacity = 1 - curalpha;

			target.FillRectangle(layoutrect, darkbrush);
			target.DrawRectangle(layoutrect, mediumbrush);	
			target.DrawText(text, format, layoutrect, mediumbrush);

			// mouseover layer
			lightbrush.Opacity = mediumbrush.Opacity = darkbrush.Opacity = curalpha;

			target.FillRectangle(layoutrect, mediumbrush);
			target.DrawText(text, format, layoutrect, lightbrush);

			target.Transform = backuptransform;
		}
	}
}
